package com.wzj.uarmor.utils;

import com.wzj.uarmor.vo.LoginUser;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @author whong on 2023/3/17
 * @Description
 */
public class TokenUtils {

    /**
     * 获得令牌
     *
     * @return {@link String}
     */
    public static String getToken(){
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = servletRequestAttributes.getRequest();
        String token = request.getHeader("token");
        return token;
    }

    /**
     * 获取登录用户
     *
     * @return {@link LoginUser}
     */
    public static LoginUser getLoginUser(){
        String token = getToken();
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = servletRequestAttributes.getRequest();
        LoginUser user = (LoginUser)request.getSession().getAttribute(token);
        return user;
    }
}
